package org.NooLab.randomgraph;



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.NooLab.randomgraph.app.IniProperties;
import org.NooLab.randomgraph.app.ProjectSpaceMaintenance;
import org.NooLab.randomgraph.app.RGraphProcessControl;
import org.NooLab.randomgraph.app.RGraphStartup;
import org.NooLab.randomgraph.components.RGraphFingerPrints;
import org.NooLab.randomgraph.components.TextCleanerIntf;
import org.NooLab.randomgraph.components.WordCleanerIntf;
import org.NooLab.randomgraph.properties.RGraphProperties;
import org.NooLab.randomgraph.storage.RGraphDataBase;
import org.NooLab.somquery.SomQueryIntf;
import org.NooLab.structures.randomgraph.GuidObject;
import org.NooLab.structures.randomgraph.RGraphPropertiesDefaultIntf;
 
import org.NooLab.utilities.files.DFutils;

public class RandomGraphFactory {

	
	public static final int _INSTANCE_TYPE_TEXT    = 1;
	public static final int _INSTANCE_TYPE_IMAGE   = 2;
	public static final int _INSTANCE_TYPE_DATA    = 3;
	public static final int _INSTANCE_TYPE_DOUBLET = 4;
	
	int currentInstanceType = _INSTANCE_TYPE_TEXT ;
	
	
	static RandomGraphFactory rgFactory;
	RGraphPropertiesDefaultIntf rgProperties;
	
	RGraphDataBase rgDataBase ; 
	static RandomGraph rGraph;
	
	String callingProcessGuid = "" ;
	GuidObject guidObj ;
	
	protected RGraphResultsProvidingEventSinkIntf clientInformer;
	protected SmallContextReplacementIntf smallContextReplacer ;
	protected WordCleanerIntf wordcleaner ;
	protected TextCleanerIntf textCleaner ;
	
	// from package org.NooLab.storequery
	protected SomQueryIntf somQuery;
	
	protected DFutils fileutil = new DFutils();
	private RGraphProcessControl rgProcessControl;
	private String inText;
	private boolean isFinished = false;
	
	 
	// ========================================================================
	private RandomGraphFactory(RGraphPropertiesDefaultIntf rgProperties) throws Exception{
		this.rgProperties = rgProperties ;

		rGraph     = new RandomGraph(this) ;
		rgDataBase = new RGraphDataBase(rgProperties) ;
		rgProcessControl = new RGraphProcessControl(this) ; 
	}
	
	public static RandomGraphFactory get(RGraphPropertiesDefaultIntf rgProperties) throws Exception {
		
		rgFactory = new RandomGraphFactory(rgProperties);
		
		return rgFactory;
	}
	// ========================================================================	
 	
	/**
	 * get an interface to the RandomGraph instance, e.g. for defining the (text) data
	 * 
	 */
	public RandomGraphIntf getRGraph(){
		
		return rGraph;
	}

	public static RandomGraph getRGraph(Class<?> clzz) {
		// 
		 // RGraphFingerPrints
		
		if (clzz==null) {
			return null;
		}
		String cname = clzz.getName() ;
		
		if (cname.toLowerCase().contains("org.noolab.randomgraph")==false){
			return null;
		}
		
		return rGraph ;
	}

	
	public void setClientInformer(RGraphResultsProvidingEventSinkIntf clientInstance) {
		clientInformer = clientInstance;
	}

	
	public void setSmallContextReplacer( SmallContextReplacementIntf replacerInstance ) {
		smallContextReplacer = replacerInstance;
	}
	
	public void setWordCleaner( WordCleanerIntf wordcleaner){
		this.wordcleaner = wordcleaner;
	}
	
	
	public void setTextCleaner(TextCleanerIntf textCleaner) {
		this.textCleaner = textCleaner;
	}
	
	public void setSomQuery(SomQueryIntf somquery) {
		somQuery = somquery;
	}
	

	public void close() {
		
	} 
	
	public int setTextData(String str){
		inText = str;
		return rGraph.setInText(inText) ;
	}
	
	public void setGuidIndices( GuidObject guidobj){
		guidObj = guidobj ;
	}
	
	public boolean start() {
		boolean rB=false;
		
		if (rGraph.getInText().length()<=50){
			rGraph.setInText(inText) ;
		}
		
		if ((inText!=null) && (inText.length()>0)){
			rGraph.start();
			rB=true;
		}
		return rB;
	}

	/**
	 * this should be used only for testing !
	 * 
	 * @param fileSource
	 * @throws IOException
	 */
	public void start(String fileSource) throws IOException {

		inText = fileutil.readFile2String(fileSource);
		// ... and start
		rGraph.setInText(inText) ;
		
		rGraph.start();
	}	

	/**
	 * 
	 * 
	 * @param instanceType
	 * @param defaultLocationPath  typically sth like [path]/iTexx/RGraph 
	 * @throws Exception 
	 */
	public static void setStartupLocations(int instanceType, String defaultLocationPath) throws Exception{
		String prj="text";
		
		
		if ( instanceType==RandomGraphFactory._INSTANCE_TYPE_TEXT ){
			prj = "text";
		}
		if ( instanceType==RandomGraphFactory._INSTANCE_TYPE_IMAGE){
			prj = "image";
		}
		if ( instanceType==RandomGraphFactory._INSTANCE_TYPE_DATA){
			prj = "data";
		}
		if ( instanceType==RandomGraphFactory._INSTANCE_TYPE_DOUBLET){
			prj = "doublet";
		}
		
		String basedir ="";
		File _fil;
		
		try{
			_fil = new File(defaultLocationPath) ;
			basedir = _fil.getAbsolutePath() ;
			defaultLocationPath = basedir ;
		}catch(Exception e){
			
		}
		
		if (DFutils.folderExists(defaultLocationPath)==false){
			DFutils.createPath(defaultLocationPath, "/");
			if (DFutils.folderExists(defaultLocationPath)==false){
				String userDir = System.getProperty("user.home");
				basedir = DFutils.createPath(userDir,defaultLocationPath+"/");
				if (DFutils.folderExists(basedir)==false){
					throw(new Exception("Please provide a proper path or directory!")) ;
				}
			}
		}
		
		IniProperties.fluidSomProjectBasePath = defaultLocationPath ;
		IniProperties.lastProjectName = prj;
		RGraphStartup.lastProjectName = prj;
		
		//getLastDataSet()
		
		IniProperties.saveIniProperties();

	}
	
	public void saveStartupTrace(int instanceType, String traceInfo) {
		String userdir, cfgTraceFile;
		
		
		userdir = fileutil.getUserDir();
		
		cfgTraceFile = fileutil.createpath(userdir, "rgraph-"+instanceType+".startup") ;
		
		fileutil.writeFileSimple(cfgTraceFile,traceInfo); 

	}

	
	
	// ------------------------------------------------------------------------
	public static boolean projectSpaceExists(String psLabel) {
		boolean rB= true;
		try {
		
			ProjectSpaceMaintenance psm = new ProjectSpaceMaintenance();
			rB = psm.projectSpaceExists( psLabel ) >= 0;
			
			
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		
		return rB;
	}

	public static void completeProjectSpace() {
		 // SomFluidFactory.projectSpaceExists() 
		String pslabel = IniProperties.lastProjectName;
		
		ProjectSpaceMaintenance psm = new ProjectSpaceMaintenance();
		
		try {
		
			psm.completeProjectSpaceDirectories() ;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public RGraphPropertiesDefaultIntf getRgProperties() {
		 
		return rgProperties ;
	}
	public RGraphDataBase getRgDataBase() {
		return rgDataBase;
	}

	public boolean isFinished() {
		return isFinished ;
	}

	public void setIsFinished(boolean flag) {
		isFinished = flag;
	}

	public ArrayList<ArrayList<?>> getResults() {

		return rGraph.getResults();
	}

	public void setRGraph(RandomGraph randomGraph) {
		rGraph = randomGraph;
	}

	public GuidObject getGuidObj() {
		return guidObj;
	}

	public void setGuidObj(GuidObject guidObj) {
		this.guidObj = guidObj;
	}

	public int getCurrentInstanceType() {
		return currentInstanceType;
	}

	public RGraphProcessControl getRgProcessControl() {
		return rgProcessControl;
	}

	public void setProcessId(String processGuid) {
		
		callingProcessGuid = processGuid ;
	}



 
}

 
